﻿<template>
  <div class="person" style="width:99%;margin-top:-25px;">
    <!-- 查询条件 -->
    <div class="query-box">
      <el-form :model="dataForm" status-icon ref="dataForm">
        <el-row>
          <el-col :span="4">
            <el-form-item label="" prop="userName">
              <el-input v-model="dataForm.userName" placeholder="请输入用户名">
                <template slot="prepend">用户名</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="oneLevelDeptId">
              <div class="depart-box">
                <div class="slect-title">一级部门</div>
                <el-select
                  v-model="dataForm.oneLevelDeptId"
                  clearable
                  filterable
                  @change="changeOne"
                >
                  <el-option
                    v-for="item in oneDept"
                    :key="item.id"
                    :label="item.deptName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="twoLevelDeptId">
              <div class="depart-box">
                <div class="slect-title">二级部门</div>
                <el-select
                  v-model="dataForm.twoLevelDeptId"
                  clearable
                  filterable
                  @change="changeTwo"
                >
                  <el-option
                    v-for="item in twoDept"
                    :key="item.id"
                    :label="item.deptName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="threeLevelDeptId">
              <div class="depart-box">
                <div class="slect-title">三级部门</div>
                <el-select
                  v-model="dataForm.threeLevelDeptId"
                  clearable
                  filterable
                  @change="changeThree"
                >
                  <el-option
                    v-for="item in threeDept"
                    :key="item.id"
                    :label="item.deptName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="duty">
              <div class="depart-box">
                <div class="slect-title">职务</div>
                <el-select v-model="dataForm.duty" clearable>
                  <el-option
                    v-for="item in duty"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="" prop="roleId">
              <div class="depart-box">
                <div class="slect-title">角色类型</div>
                <el-select v-model="dataForm.roleIds" filterable multiple clearable>
                  <el-option
                    v-for="item in roles"
                    :key="item.id"
                    :label="item.roleName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="phone">
              <el-input v-model="dataForm.phone" placeholder="请输入联系电话">
                <template slot="prepend">联系电话</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="email">
              <el-input v-model="dataForm.email" placeholder="请输入邮箱">
                <template slot="prepend">邮箱</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="realName">
              <el-input
                v-model="dataForm.realName"
                placeholder="请输入真实姓名"
              >
                <template slot="prepend">真实姓名</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="" prop="accountStatus">
              <div class="depart-box">
                <div class="slect-title">状态</div>
                <el-select v-model="dataForm.accountStatus" clearable>
                  <el-option
                    v-for="item in accountStatus"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key"
                  >
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="" prop="demoAccount">
              <el-input
                v-model="dataForm.demoAccount"
                placeholder="请输入演示账号"
              >
                <template slot="prepend">演示账号</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="from-btns">
        <kt-button
          :label="$t('action.reset')"
          perms="true"
          type="primary"
          @click="resetForm()"
        />
        <kt-button
          :label="$t('action.search')"
          perms="true"
          @click="searchForm"
        />
      </div>
    </div>
    <!-- 新增用户,导出按钮 -->
    <div class="add-btns">
      <kt-button
        :label="$t('action.add')"
        perms="Organization:Person:add"
        type="primary"
        @click="handleAdd"
      />
      <kt-button
        :label="$t('action.exportExcel')"
        perms="Organization:Person:exportExcel"
        @click="exportPerson"
        type="primary"
      />
      <kt-button
        label="刷新缓存"
        perms="true"
        :loading='reloadUserCacheLoading'
        @click="reloadUserCache"
        type="primary"
      />
    </div>
    <!--表格内容栏-->
    <Kt-table
      class="table-width"
      person
      permsEdit="Organization:Person:edit"
      permsDeletePartment="Organization:Person:deletePartment"
      permsResetPass="Organization:Person:resetPass"
      dataPermission='Organization:Person:dataPermission'
      :data="pageResult"
      :columns="columns"
      @findPage="findPage"
      @handleDataPermission='handleDataPermission'
      @handleEdit="handleEdit"
      @handleResetpass="handleResetpass"
      @changeSwitch="changeSwitch"
      @changeSwitch2="changeSwitch2"
      @handleDeletePartment="handleDeletePartment"
    >
    </Kt-table>

    <!--新增编辑界面-->
    <el-dialog
      class="add-pop personBox"
      :title="operation ? '新增' : '编辑'"
      width="30%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false">
      <el-form
        :model="dataFormAdd"
        label-width="100px"
        :rules="dataFormRules"
        ref="dataFormAdd"
        :size="size"
        label-position="right"
      >
        <!-- <el-form-item label="ID" prop="id" v-if="false">
				<el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
			</el-form-item> -->
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="dataFormAdd.userName"
            :disabled="operation ? false : true"
            placeholder="请输入用户名"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="dataFormAdd.email"
            type="email"
            auto-complete="off"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="dataFormAdd.phone"
            auto-complete="off"
            placeholder="请输入联系电话"
          ></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="dataFormAdd.realName"
            type="text"
            auto-complete="off"
            placeholder="请输入真实姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="roleId">
          <el-select
            v-model="dataFormAdd.roleIds"
            filterable
            clearable
            multiple
            placeholder="请选择角色类型"
          >
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源获取" prop="roleId">
          <el-select
            v-model="dataFormAdd.isControlResource"
            filterable
            clearable
            placeholder="是否获取资源"
          >
            <el-option
              v-for="item in isControlResource"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="一级部门" prop="oneLevelDeptId">
          <el-select
            v-model="dataFormAdd.oneLevelDeptId"
            filterable
            clearable
            @change="changeAddOne"
            placeholder="请选择一级部门"
          >
            <el-option
              v-for="item in oneDeptAdd"
              :key="item.id"
              :label="item.deptName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级部门" prop="twoLevelDeptId">
          <el-select
            v-model="dataFormAdd.twoLevelDeptId"
            filterable
             
            clearable
            placeholder="请选择二级部门"
            :disabled="dataFormAdd.oneLevelDeptId ? false : true"
            @change="changeAddTwo"
          >
            <el-option
              v-for="item in twoDeptAdd"
              :key="item.id"
              :label="item.deptName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级部门" prop="threeLevelDeptId">
          <el-select
            v-model="dataFormAdd.threeLevelDeptId"
            filterable
             
            clearable
            placeholder="请选择三级部门"
            :disabled="dataFormAdd.twoLevelDeptId ? false : true"
            @change="changeThreeFile"
          >
            <el-option
              v-for="item in threeDeptAdd"
              :key="item.id"
              :label="item.deptName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职务" prop="duty">
          <el-select
            v-model="dataFormAdd.duty"
            clearable
            placeholder="请选择职务"
             
          >
            <el-option
              v-for="item in duty"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="演示账号" prop="demoAccount">
          <el-input
            v-model="dataFormAdd.demoAccount"
            auto-complete="off"
            placeholder="请输入演示账号"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{
          $t("action.cancel")
        }}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          v-if="!dataFormAdd.id"
          :loading="editLoading"
          >{{ $t("action.submit") }}</el-button
        >
        <el-button
          :size="size"
          type="primary"
          @click.native="submitFormEdit"
          v-if="dataFormAdd.id"
          :loading="editLoading"
          >{{ $t("action.submit") }}</el-button
        >
      </div>
    </el-dialog>
    <!-- 重置密码 -->
    <el-dialog
      class="resetpassword"
      title="重置密码"
      width="30%"
      :visible.sync="resetVisible"
      :close-on-click-modal="false">
      <el-form
        label-width="110px"
        :rules="dataFormRules"
        ref="password"
        :model="dataFormAdd"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="dataFormAdd.userName"
            :disabled="true"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          class="resetpassword-pass"
          label="密码"
          prop="resetpassword"
        >
          <el-input
            v-model="dataFormAdd.acccountPassword"
            type="text"
            auto-complete="off"
            style="width: 50%;margin-right: 10%;margin-left: 1%"
          ></el-input>
          <kt-button
            :label="$t('action.autopassword')"
            @click="autopassword"
            perms="Organization:Person:resetPass"
            type="primary"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="resetVisible = false">{{
          $t("action.cancel")
        }}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="resetPwd"
          :loading="editLoading"
          >{{ $t("action.submit") }}</el-button
        >
      </div>
    </el-dialog>
      <!--权限列表-->
    <el-dialog class="rolemenu" width="50%" :visible.sync="power"
              :close-on-click-modal="false"  :title="operation ? '选择数据权限' : '选择数据权限'"  height="50%" >
      
        <div class="menu-container">
          <el-tree
          :props="props"
          :load="loadNode"
          node-key="id"
          check-on-click-node='true'
          ref="menuTree"
          highlight-current
          lazy
          default-expand-all='true'
          show-checkbox
          @node-click="handleNodeClick"
        ></el-tree>
        </div>
        <div slot="footer">
          <div style="">
            <kt-button :label="$t('action.reset')" perms="AuthorityManagement:RoleManagement:edit" authType="primary" @click="resetSelection"/>
            <kt-button :label="$t('action.submit')" perms="AuthorityManagement:RoleManagement:edit" authType="primary" @click="configResourceAuth"
                        :loading="authLoading"/>
          </div>
        </div>
    </el-dialog>
  </div>
</template>
<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import KtButton from "@/views/Core/KtButton";
import KtTable from "@/views/Core/KtTable";
import QueryBox from "@/views/Core/QueryBox";
import axios from "axios";
import { baseUrl } from "@/utils/global";
export default {
  name: "Person",
  components: {
    PopupTreeInput,
    KtTable,
    KtButton,
    QueryBox
  },
  data() {
    return {
      props: {
        label: "deptName", //这里是树结构中需显示的数据（即接口返回的需展示在页面上的参数）
        children: [],
        isLeaf: "leaf"
      },
      size: "small",
      filters: {
        name: ""
      },
      dataForm: {
        oneLevelDeptId: "",
        twoLevelDeptId: "",
        threeLevelDeptId: ""
      },
      exportPersonUrl: baseUrl + "/systemUser/exportUserInfo", //导出地址
      resetpassword: "",
      page: "person",
      columns: [
        { type: "index", label: "序号", minWidth: 50 },
        { prop: "userName", label: "用户名", minWidth: 80 },
        { prop: "realName", label: "真实姓名", minWidth: 100 },
        { prop: "oneDeptName", label: "一级部门", minWidth: 100 },
        { prop: "twoDeptName", label: "二级部门", minWidth: 100 },
        { prop: "threeDeptName", label: "三级部门", minWidth: 100 },
        { prop: "dutyName", label: "职务", minWidth: 70 },
        { prop: "roleNames", label: "角色类型", minWidth: 120 },
        { prop: "phone", label: "联系电话", minWidth: 140 },
        { prop: "email", label: "邮箱", minWidth: 150 },
        { prop: "demoAccount", label: "演示账号", minWidth: 120 }
      ],
      pageRequest: { pageNum: 1, pageSize: 8 },
      pageResult: {
        content: [],
        totalSize: ""
      },
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      resetVisible: false,
      // resetpassForm:pageResult,
      dataFormRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        realName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" }
        ],
        // roleId: [{ required: true, message: "请输入角色类型", trigger: "blur" }]
      },
      // 新增编辑界面数据
      dataFormAdd: {
        userName: "",
        realName: "",
        oneLevelDeptId: "",
        twoLevelDeptId: "",
        threeLevelDeptId: "",
        roleIds: "",
        phone: "",
        email: "",
        demoAccount: "",
        accountStatus: "",
        duty: "",
        acccountPassword: "",
        userType: "",
        isControlResource: ""
      },
      deptData: [],
      deptTreeProps: {
        label: "name",
        children: "children"
      },
      oneDept: [], //一级部门下拉框
      oneDeptAdd: [], //新增一级部门下拉框
      twoDept: [], //二级部门下拉框
      twoDeptAdd: [], //新增二级部门下拉框
      threeDept: [], //新增三级部门下拉框
      threeDeptAdd: [], //新增三级部门下拉框
      roles: [], //角色类型下拉框
      accountStatus: [], //状态下拉框
      duty: [], //职务下拉框
      isControlResource: [
        {
          value: "是",
          key: "1"
        },
        {
          value: "否",
          key: "2"
        }
      ],
      reloadUserCacheLoading: false,
      power: false,
      menuData: [],
      menuSelections: [],
      menuLoading: false,
      authLoading: false,
      checkAll: false,
      currentRoleMenus: [],
      defaultProps: {
        children: "children",
        label: "menuName"
      },
      resourceAuthUserId: "",
      allDept: []
    };
  },
  methods: {
    handleNodeClick(data) {
      console.log("node", data);
    },
    // 异步树叶子节点懒加载逻辑
    loadNode(node, resolve) {
      if (this.resourceAuthUserId) {
        this.$api.person
          .getConfigResourceAuth({ userId: this.resourceAuthUserId })
          .then(res => {
            let treeData = [];
            res.data.departmentId.forEach(item => {
              treeData.push(item);
            });
            this.$refs.menuTree.setCheckedKeys(treeData);
          });
      }

      // 一级节点处理
      if (node.level === 0) {
        this.requestTree(resolve);
      }
      // 其余节点处理
      if (node.level >= 1) {
        // 注意！把resolve传到你自己的异步中去
        this.getIndex(node, resolve);
      }
    },
    // 异步加载叶子节点数据函数
    getIndex(node, resolve) {
      this.$api.dept.findDeptLevel({ parentId: node.data.id }).then(res => {
        res.departmentInfoList.forEach(item => {
          if (!item.children) {
            item.leaf = true;
          }
        });
        let data = res.departmentInfoList;
        resolve(data);
      });
    },
    // 首次加载一级节点数据函数
    requestTree(resolve) {
      this.$api.dropdown.deptLevel2().then(res => {
        let data = res.departmentInfoList;
        resolve(data);
      });
    },
    //获取下拉框
    findDown: function() {
      this.$api.dropdown.deptLevel1().then(res => {
        this.oneDept = res.departmentInfoList;
        this.oneDeptAdd = res.departmentInfoList;
      });
      this.$api.dropdown.deptLevel2().then(res => {
        this.twoDept = res.departmentInfoList;
        this.twoDeptAdd = res.departmentInfoList;
      });
      this.$api.dropdown.deptLevel3().then(res => {
        this.threeDept = res.departmentInfoList;
        this.threeDeptAdd = res.departmentInfoList;
      });
      this.$api.role.findPage({ userType: 3 }).then(res => {
        this.roles = res.userRoleList;
      });
      this.$api.dropdown.accountStatus().then(res => {
        this.accountStatus = res.keyValueList;
      });
      this.$api.dropdown.systemDuty().then(res => {
        this.duty = res.keyValueList;
      });
      this.menuLoading = true;
      this.$api.menu.findMenuTree().then(res => {
        this.menuData = res.authorityList;
        this.menuLoading = false;
      });
    },
    //一级部门改变
    changeOne: function(value) {
      if (value) {
        this.$api.dept.findDeptLevel({ parentId: value }).then(res => {
          this.twoDept = res.departmentInfoList;
          this.dataForm.threeLevelDeptId = "";
          this.dataForm.twoLevelDeptId = "";
        });
      } else {
        this.$api.dropdown.deptLevel2().then(res => {
          this.twoDept = res.departmentInfoList;
        });
      }
    },
    //二级部门改变
    changeTwo: function(value) {
      if (value) {
        this.$api.dept.findDeptLevel({ parentId: value }).then(res => {
          this.threeDept = res.departmentInfoList;
        });
        this.$api.dept.findDeptLevel({ id: value }).then(res => {
          let deptid = res.departmentInfoList[0].parentId;
          this.dataForm.oneLevelDeptId = deptid;
          if (this.dataForm.threeLevelDeptId) {
            this.dataForm.threeLevelDeptId = "";
          }
        });
      } else {
        this.$api.dropdown.deptLevel3().then(res => {
          this.threeDept = res.departmentInfoList;
        });
      }
    },
    //三级部门改变
    changeThree: function(value) {
      if (value) {
        this.dataForm.threeLevelDeptId = value;
        this.$api.dept.findDeptLevel({ id: value }).then(res => {
          let deptid = res.departmentInfoList[0].parentId;
          this.dataForm.twoLevelDeptId = deptid;
        });
      }
    },
    //部门新增编辑下拉改变
    changeAddOne: function(value) {
      this.$api.dept.findDeptLevel({ parentId: value }).then(res => {
        this.twoDeptAdd = res.departmentInfoList;
      });
      if (value == "") {
        this.dataFormAdd.threeLevelDeptId = "";
        this.dataFormAdd.twoLevelDeptId = "";
        this.dataFormAdd.oenLevelDeptId = "";
      }
    },
    changeAddTwo: function(value) {
      this.$api.dept.findDeptLevel({ parentId: value }).then(res => {
        this.threeDeptAdd = res.departmentInfoList;
      });
      if (value == "") {
        this.dataFormAdd.threeLevelDeptId = "";
        this.dataFormAdd.twoLevelDeptId = "";
        this.dataFormAdd.oenLevelDeptId = "";
      }
    },
    changeThreeFile: function(value) {
      if (value == "") {
        this.dataFormAdd.threeLevelDeptId = "";
        this.dataFormAdd.twoLevelDeptId = "";
        this.dataFormAdd.oenLevelDeptId = "";
      }
    },
    // 获取分页数据
    findPage: function(data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest;
        this.pageRequest = Object.assign({}, { userType: 3 }, this.pageRequest);
      }
      // this.pageRequest.columnFilters = {
      //   name: { name: "name", value: this.filters.name }
      // };
      this.$api.person
        .findPage({ ...this.pageRequest, ...this.dataForm })
        .then(res => {
          this.pageResult.content = res.userInfoList;
          this.pageResult.totalSize = res.total;
          this.pageResult.content.forEach(item => {
            item.isControlResource = JSON.stringify(item.isControlResource);
          });
        })
        .then(data != null ? data.callback : "");
    },
    //重置
    resetForm: function() {
      this.dataForm = {};
      this.editLoading = false;
      this.findPage(null);
      this.findDown();
    },
    //查询
    searchForm: function() {
      this.pageRequest.pageNum = "1";
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.data = {
            userName: this.dataForm.userName,
            realName: this.dataForm.realName,
            oneLevelDeptId: this.dataForm.oneLevelDeptId,
            twoLevelDeptId: this.dataForm.twoLevelDeptId,
            threeLevelDeptId: this.dataForm.threeLevelDeptId,
            roleId: this.dataForm.roleId,
            phone: this.dataForm.phone,
            email: this.dataForm.email,
            duty: this.dataForm.duty,
            demoAccount: this.dataForm.demoAccount,
            accountStatus: this.dataForm.accountStatus
          };
          this.editLoading = true;
          let params = Object.assign({}, this.data, this.pageRequest);
          this.$api.person.findPage(params).then(res => {
            this.editLoading = false;
            if (res.result == 1) {
              this.pageResult.content = res.userInfoList;
              this.pageResult.totalSize = res.total;
            }
            this.pageResult.content.forEach(item => {
              item.isControlResource = JSON.stringify(item.isControlResource);
            });
          });
        }
      });
    },
    //新增页面展示
    handleAdd: function() {
      this.operation = true;
      this.dialogVisible = true;
      this.dataFormAdd = {
        userName: "",
        realName: "",
        oneLevelDeptId: "",
        twoLevelDeptId: "",
        threeLevelDeptId: "",
        roleId: "",
        phone: "",
        email: "",
        demoAccount: "",
        accountStatus: 1,
        duty: "",
        acccountPassword: 111111,
        userType: 3
      };
    },
    //编辑页面展示
    handleEdit: function(params) {
      this.operation = false;
      this.dialogVisible = true;
      this.dataFormAdd = Object.assign({}, params.row);
      this.dataFormAdd.duty = this.dataFormAdd.duty.toString();
      this.changeAddOne(params.row.oneLevelDeptId);
      this.changeAddTwo(params.row.twoLevelDeptId);
      this.changeThreeFile(params.row.threeLevelDeptId);
    },
    //重置密码页面展示
    handleResetpass: function(params) {
      this.resetVisible = true;
      this.dataFormAdd = Object.assign({}, params.row);
    },
    //剔除部门
    handleDeletePartment: function(params) {
      this.$confirm("确认将该用户剔除部门？", "提示", {
        type: "warning"
      }).then(() => {
        let userId = params.row.id;
        this.$api.person.eliminateDept({ userId: userId }).then(res => {
          if (res.result == 1) {
            this.$message({ message: res.msg, type: "success" });
            this.findPage(null);
          } else {
            this.$message({
              message: res.msg,
              type: "error"
            });
          }
        });
      });
    },
    //密码提交
    resetPwd: function() {
      this.$refs.password.validate(valid => {
        if (valid) {
          this.$confirm("确认重置密码吗？", "提示", {}).then(() => {
            this.data = {
              password: this.dataFormAdd.acccountPassword,
              userName: this.dataFormAdd.userName
            };
            this.editLoading = true;
            let params = Object.assign({}, this.data);
            this.$api.person.resetPwd(params).then(res => {
              this.editLoading = false;
              if (res.result == 1) {
                this.$message({ message: res.msg, type: "success" });
                this.resetVisible = false;
                this.$refs["dataFormAdd"].resetFields();
                this.findPage(null);
              } else {
                this.$message({
                  message: res.msg,
                  type: "error"
                });
              }
              this.findPage(null);
            });
          });
        }
      });
    },
    //新增提交
    submitForm: function() {
      this.$refs.dataFormAdd.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataFormAdd);
            this.$api.person.save(params).then(res => {
              this.editLoading = false;
              if (res.result == 1) {
                this.$message({ message: res.msg, type: "success" });
                this.dialogVisible = false;
                this.$refs["dataFormAdd"].resetFields();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error"
                });
              }
              this.findPage(null);
            });
          });
        }
      });
    },
    //编辑提交
    submitFormEdit: function() {
      this.$refs.dataFormAdd.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataFormAdd);
            this.dataFormAdd.roleId=''
            this.$api.person.edit(params).then(res => {
              this.editLoading = false;
              if (res.result == 1) {
                this.$message({ message: res.msg, type: "success" });
                this.dialogVisible = false;
                this.$refs["dataFormAdd"].resetFields();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error"
                });
              }
              this.findPage(null);
            });
          });
        }
      });
    },
    // 自动生成密码
    autopassword: function() {
      var password = "";
      var passLength = 8;
      var random = new Array(
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z"
      );
      //随机数
      for (let i = 0; i < passLength; i++) {
        //循环操作
        var index = Math.floor(Math.random() * 36); //取得随机数的索引（0~35）
        password += random[index];
      }
      this.dataFormAdd.acccountPassword = password;
    },
    //switch开关
    changeSwitch: function(data) {
      let userId = data.row.id;
      this.$api.person.messageStatus({ userId: userId }).then(res => {
        if (res.result == 1) {
          this.$message({ message: res.msg, type: "success" });
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.data = {
                userName: this.dataForm.userName,
                realName: this.dataForm.realName,
                oneLevelDeptId: this.dataForm.oneLevelDeptId,
                twoLevelDeptId: this.dataForm.twoLevelDeptId,
                threeLevelDeptId: this.dataForm.threeLevelDeptId,
                roleId: this.dataForm.roleId,
                phone: this.dataForm.phone,
                email: this.dataForm.email,
                duty: this.dataForm.duty,
                demoAccount: this.dataForm.demoAccount,
                accountStatus: this.dataForm.accountStatus
              };
              let params = Object.assign({}, this.data, this.pageRequest);
            }
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    changeSwitch2(data) {
      let userId = data.row.id;
      this.$api.person.updateIsContrlResource({ userId: userId }).then(res => {
        if (res.result == 1) {
          this.$message({ message: res.msg, type: "success" });
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.data = {
                userName: this.dataForm.userName,
                realName: this.dataForm.realName,
                oneLevelDeptId: this.dataForm.oneLevelDeptId,
                twoLevelDeptId: this.dataForm.twoLevelDeptId,
                threeLevelDeptId: this.dataForm.threeLevelDeptId,
                roleId: this.dataForm.roleId,
                phone: this.dataForm.phone,
                email: this.dataForm.email,
                duty: this.dataForm.duty,
                demoAccount: this.dataForm.demoAccount,
                accountStatus: this.dataForm.accountStatus
              };
              let params = Object.assign({}, this.data, this.pageRequest);
            }
          });
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    //导出
    exportPerson: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.data = {
            userName: this.dataForm.userName,
            realName: this.dataForm.realName,
            oneLevelDeptId: this.dataForm.oneLevelDeptId,
            twoLevelDeptId: this.dataForm.twoLevelDeptId,
            threeLevelDeptId: this.dataForm.threeLevelDeptId,
            roleId: this.dataForm.roleId,
            phone: this.dataForm.phone,
            email: this.dataForm.email,
            duty: this.dataForm.duty,
            demoAccount: this.dataForm.demoAccount,
            accountStatus: this.dataForm.accountStatus,
            userType: 3
          };
          let params = Object.assign({}, this.data);
          axios({
            method: "get",
            url: this.exportPersonUrl,
            params: params
          }).then(res => {
            window.open(res.request.responseURL);
          });
        }
      });
    },
    reloadUserCache() {
      this.reloadUserCacheLoading = true;
      this.$api.person.reloadUserCache().then(res => {
        if (res.result == 1) {
          this.$message({ message: res.msg, type: "success" });
          this.reloadUserCacheLoading = false;
        } else {
          this.$message({
            message: res.msg,
            type: "error"
          });
          this.reloadUserCacheLoading = false;
        }
        this.findPage(null);
      });
    },
    // 数据权限
    handleDataPermission(params) {
      this.$api.person
        .getConfigResourceAuth({ userId: params.row.id })
        .then(res => {
          let treeData = [];
          if(res.data.departmentId){
              res.data.departmentId.forEach(item => {
                treeData.push(item);
              });
          }
          this.$refs.menuTree.setCheckedKeys(treeData);
        });
      this.resourceAuthUserId = params.row.id;
      this.power = true;
    },
    // 配置资源权限
    configResourceAuth() {
      let data = {};
      let checkedKeys = this.$refs.menuTree.getCheckedNodes(false);
      // checkedKeys.forEach(item=>{
      //   console.log(item.deptLevel==2);
        
      // })
      let checkedKeysArrys = [];

      const checkedKeysArry = checkedKeys.filter(item => item.deptLevel == 3)
      // checkedKeys.splice(checkedKeys.findIndex((item,index) => item.deptLevel == 2), 1);
      checkedKeys.forEach(item => {
        
          if(item.deptLevel==3){
            checkedKeysArrys.push(item.id);
          }
      
          
        
      });
  
      data.userId = this.resourceAuthUserId;
      data.departmentId = checkedKeysArrys;
      // if (data.departmentId.length == 0) {
      //   this.$message({ message: "请选择部门！", type: "error" });
      //   return false;
      // }
      this.$api.person.configResourceAuth(data).then(res => {
        if (res.result == 1) {
          this.$message({ message: res.msg, type: "success" });
          this.power = false;
          this.findPage(null);
        } else {
          this.$message({ message: res.msg, type: "error" });
        }
      });
    },
    resetSelection() {
      this.$refs.menuTree.setCheckedKeys([]);
    }
  },
  mounted() {
    this.findDown();
  }
};
</script>
<style>
@import url("../../assets/queryBox.css");
.person input::-webkit-outer-spin-button,
.person input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
  margin: 0;
}
</style>

